package com.itheima.controller;

import com.alibaba.excel.EasyExcel;
import com.itheima.excel.StudentListener;
import com.itheima.excel.model.StudentModel;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;


    /**
     * 学员列表查询（分页查询）
     *
     * @param studentQueryParam
     * @return
     */
    @GetMapping
    public Result pageQuery(StudentQueryParam studentQueryParam) {
        PageResult<Student> pageResult = studentService.pageQuery(studentQueryParam);
        return Result.success(pageResult);
    }


    /**
     * 批量删除学员
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        studentService.delete(ids);
        return Result.success();
    }

    /*@DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids) {
        studentService.delete(ids);
        return Result.success();
    }*/


    /**
     * 添加学员
     *
     * @param student
     */
    @PostMapping
    public Result add(@RequestBody Student student) {
        studentService.add(student);
        return Result.success();
    }


    /**
     * 根据学生id查询学生信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getByStudentId(@PathVariable Integer id) {
        Student student = studentService.getByStudentId(id);
        return Result.success(student);
    }


    /**
     * 更新学员信息（修改学员）
     *
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        studentService.update(student);
        return Result.success();
    }


    /**
     * 违纪处理
     *
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable("id") Integer id, @PathVariable("score") Short score) {
        studentService.violation(id, score);
        return Result.success();
    }

    @PutMapping("/violation/{id}/null")
    public Result violation(@PathVariable("id") Integer id) {
        return Result.error("score不能为null!!!");
    }


    /**
     * 批量导入学员信息
     *
     * @param clazzId
     * @param file
     * @return
     */
    @PostMapping("/import/{clazzId}")
    public Result doImport(@PathVariable Integer clazzId, MultipartFile file) throws IOException {
        // 解析excel，调用业务层批量保存
        // 构建excel文件读取监听器
        StudentListener listener = new StudentListener(new ArrayList<>());
        // 读取excel内容
        EasyExcel.read(file.getInputStream(), StudentModel.class, listener).sheet().doRead();
        // 获取读取的结果
        List<StudentModel> stuList = listener.getStuList();

        // 批量导入
        studentService.importStudent(stuList, clazzId);
        return Result.success();
    }
}
